{include file="public/layout" /}
<body>
  <div class="layui-fluid" id="LAY-component-layer-list">
    <div class="layui-row">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="web-system">
            <div class="layui-tab layui-tab-card">
              <ul class="layui-tab-title">
                  {eq name="'Tools@index'|is_check_access" value="1"}
                  <li><a href="{:url('Tools/index')}">数据备份</a></li>
                  {/eq}

                  {eq name="'Tools@restore'|is_check_access" value="1"}
                  <li class="layui-this"><a href="{:url('Tools/restore')}">数据还原</a></li>
                  {/eq}
              </ul>
              <div class="layui-tab-content" style="padding:10px 0">
                <div class="layui-tab-item layui-show">
                  <div class="layui-row layui-col-space15">
                    <div class="layui-col-md12">
                      <div class="layui-card">
                        <div class="layui-card-body" pad15>
                          <div class="layui-form layui-border-box layui-table-view house-table" lay-filter="formTest" lay-id="admin-arctype" >
                            <div class="layui-table-box">
                              <div class="layui-table-body ">
                                <table cellspacing="0" cellpadding="0" border="0" class="layui-table layui-form" style="width: 100%" lay-skin="line">
                                  <thead >
                                    <tr>
                                      <th class="w40 tc"> 
                                        <div class="layui-table-cell w40 tc laytable-cell-checkbox">
                                          <input type="checkbox" lay-filter="checkAll" class="checkAll" lay-skin="primary">
                                          <div class="layui-unselect layui-form-checkbox" lay-skin="primary"> <i class="layui-icon layui-icon-ok"></i></div>
                                        </div>
                                      </th>
                                      <th> 
                                        <div class="layui-table-cell wauto"><span>文件名称</span></div>
                                      </th>
                                      <th class="w120 tc"> 
                                        <div class="layui-table-cell w120 tc"><span>系统版本</span></div>
                                      </th>
                                      <th class="w60 tc"> 
                                        <div class="layui-table-cell w60 tc"><span>卷号</span></div>
                                      </th>
                                      <th class="w120 tc"> 
                                        <div class="layui-table-cell w120 tc"><span>数据大小</span></div>
                                      </th>
                                      <th class="w180 tc"> 
                                        <div class="layui-table-cell w180 tc"><span>备份时间</span></div>
                                      </th>
                                      <th class="w220 tc"> 
                                        <div class="layui-table-cell w220 tc"><span>操作</span></div>
                                      </th>
                                    </tr>
                                 </thead>
                                 <tbody>
                                  {empty name="list"}
                                    <tr>
                                      <td colspan="7" class="no-data tc">
                                         没有符合条件的数据
                                      </td>
                                    </tr>
                                  {else/}
                                    {foreach name="list" item="vo" key="k" }
                                     <tr data-id="{$vo.basename}">
                                        <td>
                                           <div class="layui-table-cell w40 tc laytable-cell-checkbox">
                                              <input type="checkbox" name="ids[]" lay-filter="ids" lay-skin="primary" value="{$vo.time}">
                                           </div>
                                        </td>
                                        <td>
                                            <div class="layui-table-cell wauto">
                                                {$vo.basename}
                                            </div>
                                        </td>
                                        <td>
                                            <div class="layui-table-cell w120 tc">
                                                {$vo.version}
                                            </div>
                                        </td>
                                        <td>
                                            <div class="layui-table-cell w60 tc">
                                                {$vo.part}
                                            </div>
                                        </td>
                                        <td>
                                            <div class="layui-table-cell w120 tc">
                                                {$vo.size|format_bytes}
                                            </div>
                                        </td>
                                        <td>
                                            <div class="layui-table-cell w180 tc">
                                                {$vo.time|date="Y-m-d H:i:s",###}
                                            </div>
                                        </td>
                                        <td>
                                           <div class="layui-table-cell w220 tc right-oper"> 
                                            {eq name="$Think.const.CONTROLLER_NAME.'@new_import'|is_check_access" value="1"}
                                                <a class="layui-btn btn-edit customvar_edit" href="javascript:void(0);" data-url="{:url('Tools/new_import', array('time'=>$vo['time']))}" data-type="new_import">恢复</a>
                                            {/eq}
                                            {eq name="$Think.const.CONTROLLER_NAME.'@downFile'|is_check_access" value="1"}
                                                <a class="layui-btn btn-primary" href="{:url('Tools/downFile',array('time'=>$vo['time']))}">下载</a>
                                            {/eq}
                                            {eq name="$Think.const.CONTROLLER_NAME.'@del'|is_check_access" value="1"}
                                                <a class="layui-btn btn-primary" href="javascript:void(0);" data-url="{:url('Tools/del')}" data-id="{$vo.time}" data-type="del">删除</a>
                                            {/eq}
                                           </div>
                                        </td>
                                      </tr>
                                    {/foreach}
                                  {/empty}
                                  </tbody>
                                </table>
                              </div>
                            </div>
                            <div class="layui-table-page footer-oper">
                                <input type="checkbox" lay-filter="checkAll" class="checkAll" lay-skin="primary" >
                                {eq name="$Think.const.CONTROLLER_NAME.'@del'|is_check_access" value="1"}
                                <a class="layui-btn layui-btn-danger layui-btn-xs " data-type="batch_del" data-url="{:url('Tools/del')}">
                                <i class="layui-icon layui-icon-delete"></i>批量删除
                                </a>
                                {/eq}
                                {eq name="$Think.const.CONTROLLER_NAME.'@restoreUpload'|is_check_access" value="1"}
                                 <div class="w120" style="position: absolute; left:144px;top:10px;overflow: hidden">
                                 	<a class="layui-btn layui-btn-danger layui-btn-xs" style="position: absolute" >
                                     	<i class="layui-icon layui-icon-upload"></i>上传执行sql文件
                                    </a>
                                  	<form name="change_System" id="change_System" method="post" enctype="multipart/form-data">
                                  	   <input type="file" name="sqlfile" id="sqlfile" title="请选择…" style="position: absolute;opacity:0;width: 120px;">
                                    </form>
                                 </div>
                                {/eq}
                            </div>
                            <div class="layui-table-page">
                              <ul class="pagination">
                                <span class="rows">共 {$filenum} 条数据，共计 {$total|format_bytes}</span>
                              </ul>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
     </div>
  </div>

  <script>
  layui.config({
    base: '__SKIN__/' //静态资源所在路径
    ,version: '{$version}'
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form', 'upload'], function(){
    var $ = layui.$
    ,upload = layui.upload
    ,form = layui.form;

    // 监听复选框全选
    form.on('checkbox(checkAll)', function(data){
        if (data.elem.checked) {
            $('.checkAll').attr('checked', true);
            $('input[name*=ids]').attr('checked', true);
        } else {
            $('.checkAll').attr('checked', false);
            $('input[name*=ids]').attr('checked', false);
        }
        form.render('checkbox');
    });

    // 监听每行复选框
    form.on('checkbox(ids)', function(data){
        if (data.elem.checked && $('input[name*=ids]:checked').length == $('input[name*=ids]').length) {
            $('.checkAll').attr('checked', true);
        } else {
            $('.checkAll').attr('checked', false);
        }
        form.render('checkbox');
    });

    // 上传SQL执行
    $('#sqlfile').change(function(){
        var sqlfile = $("#sqlfile")[0].files[0];  //获取文件路径名
        var sqlfileName = sqlfile.name;
        var ext = sqlfileName.substr(sqlfileName.lastIndexOf('.')).toLowerCase();
        if ($.trim(sqlfileName) == '' || ext != '.sql') {
            $('#sqlfile').val('');
            showErrorMsg('请上传sql文件！');
            return false;
        }

        layer.confirm('此操作不可恢复，确认执行？', {
            title: false,
            btn: ['确定', '取消'] //按钮
        }, function (index) {
            layer.close(index);

            var formData = new FormData();
            formData.append('_ajax', 1);
            formData.append('sqlfile', sqlfile);

            layer_loading('正在处理');
            $.ajax({
                type: "POST",
                url: "{:url('Tools/restoreUpload')}",
                data: formData,
                dataType: 'json', //声明成功使用json数据类型回调
                //如果传递的是FormData数据类型，那么下来的三个参数是必须的，否则会报错
                cache: false,  //默认是true，但是一般不做缓存
                processData: false, //用于对data参数进行序列化处理，这里必须false；如果是true，就会将FormData转换为String类型
                contentType: false,  //一些文件上传http协议的关系，自行百度，如果上传的有文件，那么只能设置为false
                success: function (res) {
                    layer.closeAll();
                    if(res.code == 1){
                        layer.msg(res.msg, {icon: 1, time:1000}, function(){
                            window.location.reload();
                        });
                    }else{
                        showErrorMsg(res.msg);
                    }
                },
                error:function(){
                    layer.closeAll();
                    showErrorAlert();
                }
            });
        }, function (index) {
            $('#sqlfile').val('');
            layer.close(index);
        });
        return false;
    });

    /* 触发事件 */
    var active = {
        // 恢复
        new_import: function(){
            var obj = this;
            var load = layer_loading('正在处理');
            $.ajax({
                type: "POST",
                url: $(obj).attr('data-url'),
                data: {_ajax:1},
                dataType: 'json',
                success: function (res) {
                    layer.closeAll();
                    if(res.code == 1){
                        layer.msg(res.msg, {icon: 1, time:1000}, function(){
                            top.window.location.href = res.url;
                        });
                    }else{
                        showErrorMsg(res.msg);
                    }
                },
                error:function(){
                    layer.closeAll();
                    showErrorAlert();
                }
            });
            return false;
        }
        // 删除
        ,del: function(){
            delfun(this);
            return false;
        }
        // 批量删除
        ,batch_del: function(){
            batch_del(this, 'ids');
        }
    };

    $('.layui-btn').on('click', function(){
      var type = $(this).data('type');
      active[type] && active[type].call(this);
    });

  });

  </script>
{include file="public/footer" /}